package dyyx.util;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import dyyx.dto.MysqlClient;
import dyyx.dto.WebResult;

public abstract class MysqlWebUtil {

	private static final Set<String> OPS = new HashSet<>();

	private static final String OP_CONNECT = "connect";
	private static final String OP_DIS_CONNECT = "disConnect";
	private static final String OP_CLIENT_INFO = "clientInfo";
	
	


	static {

		OPS.add(OP_CONNECT);
		OPS.add(OP_DIS_CONNECT);
		OPS.add(OP_CLIENT_INFO);
		
		

		
	}
	
	
	public static void run(HttpServletRequest request, WebResult webResult) throws Exception {
		try {
			run_(request, webResult);
		} catch (Throwable e) {
			webResult.error = true;
			webResult.msg = webResult.op + " error," + e;
		}
	}
	
	
	private static void run_(HttpServletRequest request, WebResult webResult) throws Exception {
		String op = WebUtil.get(request, "op");
		String url = WebUtil.get(request, "url");
		
		String userName = WebUtil.get(request, "userName");
		String userPwd = WebUtil.get(request, "userPwd");

		if (StringUtils.isBlank(op)) {
			webResult.msg = "op blank";
			return;
		}
		if (!OPS.contains(op)) {
			webResult.msg = "op error";
			webResult.error = true;
			return;
		}
		webResult.op = op;
		

		if (OP_CONNECT.equals(op)) {

			if (StringUtils.isBlank(url)) {
				webResult.msg = "url blank";
				return;
			}

			MysqlUtil.connect(url,userName,userPwd);

			webResult.msg = op + " success," + MysqlUtil.getClient();

			return;
		}

		if (OP_DIS_CONNECT.equals(op)) {

			MysqlUtil.disconnect();

			webResult.msg = op + " success";

			return;
		}

		if (OP_CLIENT_INFO.equals(op)) {

			MysqlClient mysqlClient = MysqlUtil.getClient();
			
			
			webResult.msg = mysqlClient.toString();

			return;
		}
		
		
		
		
	}
	


}
